package pit.com.ordermobilesystem.a;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.http.Group;
import pit.com.ordermobilesystem.http.HttpApiException;
import pit.com.ordermobilesystem.http.OrderSystemHttpService;
import pit.com.ordermobilesystem.http.Response;
import pit.com.ordermobilesystem.model.Category;
import pit.com.ordermobilesystem.model.Item;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.tabhost.TabGroupActivity;
import pit.com.ordermobilesystem.util.OrderConstant;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class UpdateActivity extends Activity implements OrderConstant{
	private ImageView imv_update;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update);
		imv_update = (ImageView) findViewById(R.id.imv_update);
		imv_update.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				
				getContentResolver().delete(OrderMobileProvider.ITEM_CONTENT_URI, null, null);
				getContentResolver().delete(OrderMobileProvider.CATEGORY_CONTENT_URI, null, null);

				new LoadingCategory().execute("");
				
			}
		});
	}

	
	class LoadingCategory extends AsyncTask<String, Void, Void> {

		private Exception exception = null;
		ProgressDialog dialog;
		Response respone;
		Group<Category> list;
		Group<Item> listItems;
		String notice="Loading Category. Please wait...";
		protected void onPreExecute() {
			TabGroupActivity parentActivity = (TabGroupActivity)getParent();
				dialog = new ProgressDialog(parentActivity);
				dialog.setMessage(notice);
				dialog.setCanceledOnTouchOutside(false);
				dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
							
							@Override
							public void onCancel(DialogInterface dialog) {
								LoadingCategory.this.cancel(true);
								dialog.dismiss();
							}


				});
				dialog.show();


		}
		
		

		@Override
		protected void onProgressUpdate(Void... values) {
			dialog.setMessage(notice);
		}



		protected Void doInBackground(final String... args) {
			OrderSystemHttpService httpService=new OrderSystemHttpService(SERVER_HOST,PORT);
			try {
				list=httpService.downloadCategoryGroup();
				ContentValues[] listValues=new ContentValues[list.size()];
				
				for(int i=0;i<list.size();i++){
					listValues[i]=OrderMobileProvider.getcontentValuesCategory(list.get(i));
				}
				//Insert into database
				getContentResolver().bulkInsert(OrderMobileProvider.CATEGORY_CONTENT_URI, listValues);
				
				notice="Loading Items, please waiting ....";		
				setProgress(1);
				
				listItems=httpService.downloadItemGroup();
				ContentValues[] listValuesItems=new ContentValues[listItems.size()];
				
				for(int i=0;i<listItems.size();i++){
					listValuesItems[i]=OrderMobileProvider.getContentValuesItem(listItems.get(i));
				}
				//Insert into database
				getContentResolver().bulkInsert(OrderMobileProvider.ITEM_CONTENT_URI, listValuesItems);				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				exception=e;
			} catch (HttpApiException e) {
				// TODO Auto-generated catch block
				exception=e;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				exception=e;
			}
			return null;
		}

		protected void onPostExecute(final Void unused) {
			dialog.cancel();

			if(exception==null){
				if(list!=null && list.size()!=0){
					Toast.makeText(getApplicationContext(), "Bạn đã update thành công.", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(getApplicationContext(), "Bạn đã update thành công.", Toast.LENGTH_LONG).show();
				}
			}else{
				Toast.makeText(getApplicationContext(), "Bạn chưa thể update được.", Toast.LENGTH_LONG).show();
			}

		}

	}
	
}
